<?php


/**
 * 下午茶申请单类
 * @package  htdocs 
 * @subpackage  quanliadmin
 * @author   allen.qiang  <allen.qiang@fistforward.com> 2015-01-14 allen.qiang FUN-AC-0028-下午茶项目
 * @version 1.0
 */

class sysStock{


    /**
     * 获取下午茶库存信息
     * @param $arg_array
     * @return array|null
     */
    public function getStockList(&$arg_array){
    	global $G_PGLOB;
        //sql字符串
        $sql = ' AND total_apply_amount>0 ';
        
        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();


        //库存编号
        if (isset($arg_array['stock_id']) && !empty($arg_array['stock_id'])) {
            $sql .= ' AND stock_id = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['stock_id'];
        }

        //配送日期
        if (isset($arg_array['stock_date']) && !empty($arg_array['stock_date'])) {
            $sql .= ' AND stock_date = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['stock_date'];
        }

        //配送站
        if (isset($arg_array['distribution_id']) && !empty($arg_array['distribution_id'])) {
            $sql .= ' AND distribution_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['distribution_id'];
        }

        //商品编码
        if (isset($arg_array['product_code']) && !empty($arg_array['product_code'])) {
            $sql .= ' AND product_code = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['product_code'];
        }

        //商品名称
        if (isset($arg_array['product_name']) && !empty($arg_array['product_name'])) {
            $sql .= ' AND product_name like ?';
            $params_type .= 's';
            $params_array[] =  '%'.$arg_array['product_name'].'%';
        }

        if ($sql) {
            $sql = ' WHERE ' . substr($sql, 4) ;
        }

        try {

            if (isset($arg_array['rp'])) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM dunch_stock  ' . $sql;
                $count_array = &$G_PGLOB->DBCA->stmt_query($search_sql, $params_type, $params_array);

                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }


            //查询分类信息
            $search_sql = "SELECT * FROM `dunch_stock` ".$sql;
            $temp_item_array = &$G_PGLOB->DBCA->stmt_query($search_sql, $params_type, $params_array);

            if (!$temp_item_array)
                return null;

            $item_array = array();

            foreach($temp_item_array as &$item) {
            	
                $item_array[] = array(
                    'creator' => $item->creator,
                    'createTime' => $item->createTime,
                    'stock_id' => $item->stock_id,
                    'brandID' => $item->brandID,
                    'distribution_id' => $item->distribution_id,
                    'distribution_name' => $item->distribution_name,
                    'stock_date' => $item->stock_date,
                    'product_code' => $item->product_code,
                    'product_name' => $item->product_name,
                    'total_apply_amount' => $item->total_apply_amount,
                    'total_paid_amount' => $item->total_paid_amount
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 获取下午茶库存商品出入库详情
     * @param $arg_array
     * @return array|null
     */
    public function getStockDetail(&$arg_array){
        global $G_PGLOB;
        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();


        //库存编号
        if (isset($arg_array['stock_id']) && !empty($arg_array['stock_id'])) {
            $sql .= ' AND stock_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['stock_id'];
        }

        //类型
        if (isset($arg_array['log_type']) && !empty($arg_array['log_type'])) {
            $sql .= ' AND log_type = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['log_type'];
        }

        if ($sql) {
            $sql = ' WHERE ' . substr($sql, 4) ;
        }

        try {

            //查询出入库日志信息
            $search_sql = "SELECT * FROM `dunch_stock_log` ".$sql;
            $temp_item_array = &$G_PGLOB->DBCA->stmt_query($search_sql, $params_type, $params_array);

            if (!$temp_item_array)
                return null;

            $item_array = array();

            foreach($temp_item_array as &$item) {

                $item_array[] = array(
                    'creator' => $item->creator,
                    'createTime' => $item->createTime,
                    'stock_id' => $item->stock_id,
                    'log_type' => $item->log_type==1?'入库':'异动',
                    'amount' => $item->amount,
                    'remark' => $item->remark
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    //修改库存
    public function modStock(){
        global $G_SHOP;

        //取值
        $stock_id=$_REQUEST["stock_id"]; //库存编号
        $amount=$_REQUEST["amount"]; //修改数量
        $log_type=$_REQUEST["log_type"]; //类型
        $remark=$_REQUEST["remark"]; //备注
        $custID=$G_SHOP->currentUserID; //后台用户ID

        try{
            $G_SHOP->DBCA->beginTransaction();

            //添加库存表数据
            $dunch_stock = new entity('dunch_stock');
            $dunch_stock->stock_id = $stock_id;
            if($G_SHOP->DBCA->fetch($dunch_stock)){
                $dunch_stock->total_paid_amount += $amount;
            }
            if (!$G_SHOP->DBCA->save($dunch_stock)) {
                throw new Exception('页面已过期，请刷新页面重试', 404);
            }

            //添加入库日志
            $dunch_stock_log = new entity('dunch_stock_log');
            $dunch_stock_log->creator = $custID;
            $dunch_stock_log->stock_id = $dunch_stock->stock_id;
            $dunch_stock_log->log_type = $log_type;
            $dunch_stock_log->amount = $amount;
            $dunch_stock_log->remark= $remark;
            if (!$G_SHOP->DBCA->save($dunch_stock_log)) {
                throw new Exception('页面已过期，请刷新页面重试', 404);
            }

            $G_SHOP->DBCA->commit();
            return array( 'code' => 200, 'message' => '修改库存成功！' );
        }
        catch (Exception $e) {

            $G_SHOP->DBCA->rollBack();

            if ($e->getCode() > 0)
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            else {
                error_log($e->getMessage());
                return array( 'code' => 700, 'message' => '系统错误' );
            }
        }
    }


}